/*
 * Copyright (C) 2012 Le Tuan Anh <anh.takaji@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ezcms.client.sample;

import ezcms.client.ConferenceServiceDelegate;
import ezcms.client.UserServiceDelegate;
import ezcms.dto.Conference;
import ezcms.dto.User;
import ezcms.exceptions.LookupException;
import java.util.List;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Le Tuan Anh <anh.takaji@gmail.com>
 */
public class Application {
    
    public static void main(String[] args) throws Exception {
        sampleDelegateCode();
        System.out.println("--- End delegate test ---");
        testMDB();
        System.out.println("---- End sample ---");
        System.exit(0);
    }
    
    private static void sampleDelegateCode() throws LookupException {
        UserServiceDelegate userDelegate = new UserServiceDelegate();
        ConferenceServiceDelegate conferenceDelegate = new ConferenceServiceDelegate();
        User u = null;
        try {
            u = userDelegate.getUser("admin@localhost");
            boolean auth = userDelegate.authenticate(new User("admin@localhost", "123456"));
            
            List<Conference> list = conferenceDelegate.getActiveConferences();
            for (Conference obj : list) {
                System.out.println(obj);
                conferenceDelegate.saveConference(obj);
            }
            
            System.out.println(auth);
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("Error!");
        }
        System.out.println(u);
    }
    
    private static void testMDB() throws NamingException, JMSException {
        InitialContext context = new InitialContext();
        Queue queue = (Queue) context.lookup("jms/ezcms.queue.mail.notifier");
        QueueConnectionFactory factory = (QueueConnectionFactory) context.lookup("jms/ezcms.queue.mail.notifier.ConnectionFactory");
        QueueConnection connection = (QueueConnection) factory.createQueueConnection();
        QueueSession session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
        QueueSender sender = session.createSender(queue);
        MapMessage msg = session.createMapMessage();
        msg.setString("Title", "Hi there");
        msg.setString("Body", "How are you?");
        sender.send(msg);
        sender.close();
        session.close();
        connection.close();
        System.out.println("Message sent!");
//        System.exit(0);
    }
}
